package com.bins.springboot.dubbo.api.thread;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class SpiderTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newWorkStealingPool(100);
        for(int i = 0; i < 1; i++){
            final int a = i;
            Future<?> submit = executorService.submit(() -> {
                StringBuilder postData = new StringBuilder();
                try {
                    URL url = new URL("https://qingbuyaohaixiu.com/tag/24/?page=24");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.addRequestProperty("Connection","Keep-Alive");
                    conn.addRequestProperty("Charset","UTF-8");
                    conn.setDoOutput(true);
                    conn.setUseCaches(false);
                    conn.setConnectTimeout(5*1000);
                    conn.setRequestProperty("user-agent",
                            "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.7 Safari/537.36");
                    conn.connect();


                    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){
                        BufferedInputStream bufferedOutputStream = new BufferedInputStream(conn.getInputStream());
                        byte[] bytes = new byte[1024];
                        File file = new File("D:\\ou.jpeg");
                        if(!file.exists()){
                            file.createNewFile();
                        }
                        int j = 0;
                        try(OutputStream outputStream = new FileOutputStream(file, true)) {
                            while ((j = bufferedOutputStream.read(bytes)) != -1){
                                outputStream.write(bytes,0, j);
                            }
                            bufferedOutputStream.close();


                        }catch (Exception e){
                            e.printStackTrace();
                        }



                        System.out.println(postData);
                        System.out.println(a);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            });
        }
        executorService.shutdown();
        while (true){
            if(executorService.isTerminated())
                break;
        }
    }
}
